package com.stylefeng.guns.modular.system.controller.server;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.ProjectConfigMapper;
import com.stylefeng.guns.common.persistence.model.ProjectConfig;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.dao.ProjectConfigDao;
import com.stylefeng.guns.modular.system.warpper.ProjectConfigWarpper;
import com.stylefeng.guns.modular.utils.DBConnect;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lmshe
 * @create 2018-07-04 14:12
 * @desc
 */
@Controller
@RequestMapping("/projectConfig")
public class ProjectConfigController extends BaseController {

    private static String PREFIX = "/system/projectConfig/";

    @Resource
    private ProjectConfigDao projectConfigDao;

    @Resource
    private ProjectConfigMapper projectConfigMapper;
    /**
     * 跳转列表页面
     * @return
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "project_config.html";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list() {
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        List<Map<String, Object>> result = projectConfigDao.getProjectConfigList(page, page.getOrderByField(), page.isAsc());
        page.setRecords((List<Map<String, Object>>) new ProjectConfigWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/project_config_add")
    public String projectConfigAdd() {
        return PREFIX + "project_config_add.html";
    }

    /**
     * 添加
     * @param model
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增项目配置",key = "project_config")
    public Object add(ProjectConfig model) {
        model.insert();
        return super.SUCCESS_TIP;
    }

    /**
     * 跳转到修改页面
     */
    @RequestMapping("/project_config_update/{id}")
    public String projectConfigUpdate(@PathVariable Integer id, Model model) {
        ProjectConfig  projectConfig = this.projectConfigMapper.selectById(id);
        model.addAttribute("projectConfig",projectConfig);
        LogObjectHolder.me().set(projectConfig);
        return PREFIX + "project_config_edit.html";
    }

    /**
     * 修改
     * @param model
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改项目配置",key = "project_config")
    public Object update(ProjectConfig model) {
        this.projectConfigMapper.updateById(model);
        return super.SUCCESS_TIP;
    }

    /**
     * 测试连接
     * @param projectConfigId
     * @return
     */
    @RequestMapping(value = "/test_connection")
    @ResponseBody
    @BussinessLog(value = "测试连接",key = "projectConfigId")
    public Object testConnection(@RequestParam Integer projectConfigId) {
        ProjectConfig  projectConfig = this.projectConfigMapper.selectById(projectConfigId);
        if(!DBConnect.testConnection(projectConfig)){
            throw new BussinessException(BizExceptionEnum.DB_CONNECT_ERROR);
        }
        return super.SUCCESS_TIP;
    }
}
